package com.example.springsecurityoauth2demo.service.impl;

import com.example.springsecurityoauth2demo.model.pojo.SecurityUser;
import com.example.springsecurityoauth2demo.model.pojo.User;
import com.example.springsecurityoauth2demo.service.MyUserService;
import com.example.springsecurityoauth2demo.service.UserService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author Liusr
 * @date 2024/3/1
 * @package com.example.springsecurityoauth2demo.service.impl
 * @Description:
 */
@Service
public class MyUserServiceImpl implements UserDetailsService, MyUserService {

	@Resource
	private PasswordEncoder passwordEncoder;
	@Resource
	private UserService userService;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = userService.getUserByUserName(username);
		String name = user.getName();
		String password = user.getPassword();
		String authority = user.getAccount();
		return new SecurityUser(name, password, AuthorityUtils.commaSeparatedStringToAuthorityList(authority));
	}
}
